<?php

class KullanicisController extends AppController {
	public $helpers = array('Html', 'Form');
	public $components = array('Paginator');
	public $paginate = array(
			'limit' => 5,
			'order' => array(
					'Kullanici.username' => 'asc'
			)
	);
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('login', 'logout', '/menus');
	}

	public function login() {
		CakeLog::write('debug',"LOGIN cagrildi");
		debug(FULL_BASE_URL, true);
		if ($this->request->is('post')) {
			CakeLog::write('debug',"LOGIN request post");
			if ($this->Auth->login()) {
				CakeLog::write('debug',"LOGIN olundu");
				
				$this->Session->write('xxxx', 'Green');
				CakeSession::write("xxxx", "Green");
				return $this->redirect($this->Auth->redirect());
			}
			$this->Session->setFlash(__('Invalid username or password, try again'));
		}
	}

	public function xxx () {
		$this->autoRender=false;
		debug($this->getKatilimciId(), true);
	}
	
	public function loginTrue() {
		$this->autoRender = true;
		$this->layout=null;
		$this->redirect("kullanicis");
	}

	public function logout() {
		$this->Session->delete('KULLANICI');
		return $this->redirect($this->Auth->logout());
	}

	public function index() {
		$this->autoRender = true;
		$this->layout=null;
		/*
		$this->Kullanici->recursive = 0;
		$this->set('kullanicis', $this->paginate());
		*/
		$this->set('projeId', $this->getKatilimci()['proje_id']);
		
	}

	public function view($id = null) {
		$this->Kullanici->id = $id;
		if (!$this->Kullanici->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->set('kullanici', $this->Kullanici->read(null, $id));
	}


	public function save() {
		$this->layout = null ;
		$this->autoRender = false;
		if ($this->request->is('post')) {
			$this->Kullanici->create();
			$result = $this->Kullanici->save($this->request->data);
			if ($result) {
				echo "{msg:'Kullanici Kaydetme Tamamlandi', success: true}";
			} else {
				echo "{msg:'hatalı', success: false}";
			}
		}
	}

// 	public function add() {
// 		if ($this->request->is('post')) {
// 			$this->Kullanici->create();
// 			if ($this->Kullanici->save($this->request->data)) {
// 				$this->Session->setFlash(__('The user has been saved'));
// 				return $this->redirect(array('action' => 'login'));
// 			}
// 			$this->Session->setFlash(
// 					__('The user could not be saved. Please, try again.')
// 			);
// 		}
// 	}


	public function load($id = null) {
		$this->layout='ajax' ;
		$this->Kullanici->id = $id;
		if (!$this->Kullanici->exists()) {
			//throw new NotFoundException(__('Invalid user'));
		}
		$this->set('kullanici', $this->Kullanici->read(null, $id));
	}
	
	public function edit($id = null) {
		$this->Kullanici->id = $id;
		if (!$this->Kullanici->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Kullanici->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				return $this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(
					__('The user could not be saved. Please, try again.')
			);
		} else {
			$this->request->data = $this->Kullanici->read(null, $id);
			unset($this->request->data['Kullanici']['password']);
		}
	}

	public function listKullanici () {
		$this->layout = null ;
		$this->Paginator->settings = $this->paginate;
		$this->Paginator->settings = array(
				'conditions' => array('Kullanici.username LIKE' => $this->request->query('username') . '%', 'Kullanici.eposta LIKE' => $this->request->query('eposta') . '%'),
				'limit' => $this->request->query('limit'),
				'page' => $this->request->query('page')
		);
	
	
	
		if ($this->request->query('sort')) {
			$sortArr = json_decode($this->request->query['sort'], true);
			if (count($sortArr) == 1) {
				$property =  $sortArr[0]['property'];
				$direction =  $sortArr[0]['direction'];
				$this->Paginator->settings['order'] =  array(
						'Kullanici.' . $property => $direction
				);
			}
		}
	
		// similar to findAll(), but fetches paged results
		$data = $this->Paginator->paginate('Kullanici');
		//$this->Paginator->counter();//$this->Paginator->paging['count']; //$this->Paginator->find('count');
		//echo $count . "xxxx";
		$count = $this->params['paging']['Kullanici']['count'];
		//debug($data, true);
		$this->set('kullanicis', $data);
		$this->set('count', $count);
	}
	
	public function delete($id = null) {
		$this->autoRender = false;
		$this->layout = 'ajax';
	
		$this->Kullanici->id = $id;
	
		if ($this->Kullanici->delete()) {
			echo "{msg:'Silme Başarıyla Tamamlandı', success: true}";
		} else {
			echo "{msg:'hatalı', success: false}";
		}
	}
	
// 	public function delete($id = null) {
// 		$this->request->onlyAllow('post');

// 		$this->Kullanici->id = $id;
// 		if (!$this->Kullanici->exists()) {
// 			throw new NotFoundException(__('Invalid user'));
// 		}
// 		if ($this->Kullanici->delete()) {
// 			$this->Session->setFlash(__('User deleted'));
// 			return $this->redirect(array('action' => 'index'));
// 		}
// 		$this->Session->setFlash(__('User was not deleted'));
// 		return $this->redirect(array('action' => 'index'));
// 	}
	
	/**
	 * kayit view'ini gösterir
	 */
	public function kayitV () {
		$this->autoRender = true;
		$this->layout=null;
		/*
		 $this->Kullanici->recursive = 0;
		$this->set('kullanicis', $this->paginate());
		*/
		$this->set('projeId', $this->request->query('_pid'));
	}

}